#include "battlegroup.h"
#include "ui_battlegroup.h"
#include "qpainter.h"
#include "itemselectpage.h"
#include "DataBase/mysql.h"
#include "constant.h"
BattleGroup::BattleGroup(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BattleGroup)
{
    ui->setupUi(this);
    initTableView_All();
    initTableView_A();
    initTableView_B();
    initTableView_C();
    initTableView_D();
//    initTableView_AllData();
//    initServer();
    if(Constant::ShowFullScreen)
        this->showFullScreen();

}

BattleGroup::~BattleGroup()
{
    delete ui;
}
//void BattleGroup::initServer()
//{
//    Server::instance();
//    connect(this,SIGNAL(command_toClient(int,QJsonObject)),Server::instance(),SLOT(writeMsgToClient(int,QJsonObject)));
//    connect(Server::getInstance(),SIGNAL(updateMsg(QString, QString)),this,SLOT(receivedMsg(QString, QString)));

//    connect(Server::getInstance(),SIGNAL(updateMsg(QJsonObject&)),this,SLOT(receivedMsg(QJsonObject&)));
//    connect(Server::getInstance(),SIGNAL(updateClient(QString)),this,SLOT(addNewClient(QString)));
//    connect(Server::getInstance(),SIGNAL(removeClient(QString)),this,SLOT(removeClient(QString)));


//}
void BattleGroup::getUserTableInfo(QHashUserTable m_user)
{
    this->m_userTableHash=m_user;
    RefreshTable();

}
void BattleGroup::paintEvent(QPaintEvent *event)
{
    //通过这种方式可以将图片平铺到窗口上
    //使用样式表可以很方便设置界面，而且非常高效，还能让界面和逻辑分离
    //不过在本例中由于是顶层窗口，所以直接设置样式表也不会显示。
    //所以样式表比较适合子窗口来使用。
    QPainter painter(this);
    painter.drawPixmap(rect(),QPixmap(":/images/share/selectmodel_page_bg2.png"),QRect());
}




void BattleGroup::initTableView_AllData()
{
    for(int i=0;i<17;i++)
    {
        USER_TABLE_DATA userdata;
        userdata.IP="192.168.31.10"+QString::number(i);
        userdata.nNo="00"+QString::number(i);
        userdata.nAge=31+i;
        userdata.nSex="男";
        userdata.sName="admin";
        userdata.DeviceState="连接";
        userdata.socketDescriptor=1;
        m_userTableHash.insert(userdata.IP,userdata);
    }
    RefreshTable();

}

void BattleGroup::RefreshTable()
{
    if(m_pTableModel_All->rowCount() > 0)
    {
        m_pTableModel_All->removeRows(0, m_pTableModel_All->rowCount());
    }

    QHashUserTableIterator i = m_userTableHash.begin();
    int row = 0;
    for(; i != m_userTableHash.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();

        //可以通过insertRow和setData增加数据
        m_pTableModel_All->insertRow(row, QModelIndex());
        m_pTableModel_All->setData(m_pTableModel_All->index(row, COLUMN_CHECKBOX), "");
        m_pTableModel_All->setData(m_pTableModel_All->index(row, COLUMN_NO), row+1);
        m_pTableModel_All->setData(m_pTableModel_All->index(row, COLUMN_IP), data.IP);
        m_pTableModel_All->setData(m_pTableModel_All->index(row, COLUMN_ID), data.sName);
        m_pTableModel_All->setData(m_pTableModel_All->index(row, COLUMN_NAME), data.nNo);
        m_pTableModel_All->setData(m_pTableModel_All->index(row, COLUMN_SEX), data.nSex);
        m_pTableModel_All->setData(m_pTableModel_All->index(row, COLUMN_AGE), data.nAge);
        m_pTableModel_All->setData(m_pTableModel_All->index(row,COLUMN_DEVICE),data.DeviceState);


        //也可以通过setItem增加数据
        //m_pTableModel->setItem(row, COLUMN_CHECKBOX, new QStandardItem(""));

        // 设置全部列居中显示
        for(int j = 0; j <= 7; j++)
        {
            m_pTableModel_All->item(row, j)->setTextAlignment(Qt::AlignCenter);
        }
        row++;
    }
}

void BattleGroup::RefreshTableA()
{
    if(m_pTableModel_A->rowCount() > 0)
    {
        m_pTableModel_A->removeRows(0, m_pTableModel_A->rowCount());
    }

    QHashUserTableIterator i = m_userTableHash_A.begin();
    int row = 0;
    for(; i != m_userTableHash_A.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();

        //可以通过insertRow和setData增加数据
        m_pTableModel_A->insertRow(row, QModelIndex());
        m_pTableModel_A->setData(m_pTableModel_A->index(row, COLUMN_CHECKBOX), "");
        m_pTableModel_A->setData(m_pTableModel_A->index(row, COLUMN_NO), row+1);
        m_pTableModel_A->setData(m_pTableModel_A->index(row, COLUMN_IP), data.IP);
        m_pTableModel_A->setData(m_pTableModel_A->index(row, COLUMN_ID), data.sName);
        m_pTableModel_A->setData(m_pTableModel_A->index(row, COLUMN_NAME), data.nNo);
        m_pTableModel_A->setData(m_pTableModel_A->index(row, COLUMN_SEX), data.nSex);
        m_pTableModel_A->setData(m_pTableModel_A->index(row, COLUMN_AGE), data.nAge);
        m_pTableModel_A->setData(m_pTableModel_A->index(row,COLUMN_DEVICE),data.DeviceState);
        // 设置全部列居中显示
        for(int j = 0; j <= 7; j++)
        {
            m_pTableModel_A->item(row, j)->setTextAlignment(Qt::AlignCenter);
        }
        row++;
    }
}

void BattleGroup::RefreshTableB()
{
    if(m_pTableModel_B->rowCount() > 0)
    {
        m_pTableModel_B->removeRows(0, m_pTableModel_B->rowCount());
    }

    QHashUserTableIterator i = m_userTableHash_B.begin();
    int row = 0;
    for(; i != m_userTableHash_B.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();

        //可以通过insertRow和setData增加数据
        m_pTableModel_B->insertRow(row, QModelIndex());
        m_pTableModel_B->setData(m_pTableModel_B->index(row, COLUMN_CHECKBOX), "");
        m_pTableModel_B->setData(m_pTableModel_B->index(row, COLUMN_NO), row+1);
        m_pTableModel_B->setData(m_pTableModel_B->index(row, COLUMN_IP), data.IP);
        m_pTableModel_B->setData(m_pTableModel_B->index(row, COLUMN_ID), data.sName);
        m_pTableModel_B->setData(m_pTableModel_B->index(row, COLUMN_NAME), data.nNo);
        m_pTableModel_B->setData(m_pTableModel_B->index(row, COLUMN_SEX), data.nSex);
        m_pTableModel_B->setData(m_pTableModel_B->index(row, COLUMN_AGE), data.nAge);
        m_pTableModel_B->setData(m_pTableModel_B->index(row,COLUMN_DEVICE),data.DeviceState);
        // 设置全部列居中显示
        for(int j = 0; j <= 7; j++)
        {
            m_pTableModel_B->item(row, j)->setTextAlignment(Qt::AlignCenter);
        }
        row++;
    }
}

void BattleGroup::RefreshTableC()
{
    if(m_pTableModel_C->rowCount() > 0)
    {
        m_pTableModel_C->removeRows(0, m_pTableModel_C->rowCount());
    }

    QHashUserTableIterator i = m_userTableHash_C.begin();
    int row = 0;
    for(; i != m_userTableHash_C.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();

        //可以通过insertRow和setData增加数据
        m_pTableModel_C->insertRow(row, QModelIndex());
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_CHECKBOX), "");
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_NO), row+1);
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_IP), data.IP);
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_ID), data.sName);
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_NAME), data.nNo);
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_SEX), data.nSex);
        m_pTableModel_C->setData(m_pTableModel_C->index(row, COLUMN_AGE), data.nAge);
        m_pTableModel_C->setData(m_pTableModel_C->index(row,COLUMN_DEVICE),data.DeviceState);
        // 设置全部列居中显示
        for(int j = 0; j <= 7; j++)
        {
            m_pTableModel_C->item(row, j)->setTextAlignment(Qt::AlignCenter);
        }
        row++;
    }
}


void BattleGroup::RefreshTableD()
{
    if(m_pTableModel_D->rowCount() > 0)
    {
        m_pTableModel_D->removeRows(0, m_pTableModel_D->rowCount());
    }

    QHashUserTableIterator i = m_userTableHash_D.begin();
    int row = 0;
    for(; i != m_userTableHash_D.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();

        //可以通过insertRow和setData增加数据
        m_pTableModel_D->insertRow(row, QModelIndex());
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_CHECKBOX), "");
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_NO), row+1);
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_IP), data.IP);
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_ID), data.sName);
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_NAME), data.nNo);
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_SEX), data.nSex);
        m_pTableModel_D->setData(m_pTableModel_D->index(row, COLUMN_AGE), data.nAge);
        m_pTableModel_D->setData(m_pTableModel_D->index(row,COLUMN_DEVICE),data.DeviceState);
        // 设置全部列居中显示
        for(int j = 0; j <= 7; j++)
        {
            m_pTableModel_D->item(row, j)->setTextAlignment(Qt::AlignCenter);
        }
        row++;
    }
}
void BattleGroup::initTableView_All()
{
    //自定义表头操作
    m_pTableHeader_All = new TableViewHeader(Qt::Horizontal, this);//表头
    m_pTableHeader_All->setStretchLastSection(true);//表头最后一列填充
    ui->tableView->setHorizontalHeader(m_pTableHeader_All);//设置表头

    //创建model（目前使用的是标准数据模型，可以自定义数据模型），绑定model
    m_pTableModel_All = new QStandardItemModel(this);
    ui->tableView->setModel(m_pTableModel_All);//设置model
    ui->tableView->verticalHeader()->hide();


    //创建表头信息
    QStringList headerList;
    headerList << " " << tr("No.")   << tr("IP")<< tr("用户名") << tr("姓名") << tr("性别") << tr("年龄")<<tr("手环");
    m_pTableModel_All->setHorizontalHeaderLabels(headerList);


    //单击
    connect(ui->tableView, &QTableView::clicked,this, &BattleGroup::on_tableViewClick);
    //双击
    connect(ui->tableView, &QTableView::doubleClicked,this, &BattleGroup::on_tableViewDbClick);

    //标题头排序
    connect(ui->tableView->horizontalHeader(), &QHeaderView::sectionClicked,this, &BattleGroup::on_tableViewSort);
    //全选
    connect(m_pTableHeader_All, &TableViewHeader::stateChanged,this, &BattleGroup::on_tableViewSelectAllCheckBoxClick);

    //设置列宽
    setColumnWidth();

}
void BattleGroup::initTableView_A()
{
    //自定义表头操作
    m_pTableHeader_Chlid = new TableViewHeader(Qt::Horizontal, this);//表头
    m_pTableHeader_Chlid->setStretchLastSection(true);//表头最后一列填充
    ui->tableView_A->setHorizontalHeader(m_pTableHeader_Chlid);//设置表头

    //创建model（目前使用的是标准数据模型，可以自定义数据模型），绑定model
    m_pTableModel_A = new QStandardItemModel(this);
    ui->tableView_A->setModel(m_pTableModel_A);//设置model
    ui->tableView_A->verticalHeader()->hide();


    //创建表头信息
    QStringList headerList;
    headerList << " " << tr("No.")   << tr("IP")<< tr("用户名") << tr("姓名") << tr("性别") << tr("年龄")<<tr("手环");
    m_pTableModel_A->setHorizontalHeaderLabels(headerList);

    //双击撤回
    connect(ui->tableView_A, &QTableView::doubleClicked,this, &BattleGroup::on_tableViewADbClick);

    //设置列宽
    ui->tableView_A->setColumnWidth(COLUMN_CHECKBOX,0);
    ui->tableView_A->setColumnWidth(COLUMN_NO,50);
    ui->tableView_A->setColumnWidth(COLUMN_IP, 250);
    ui->tableView_A->setColumnWidth(COLUMN_ID, 250);

    ui->tableView_A->setColumnWidth(COLUMN_NAME, 250);
    ui->tableView_A->setColumnWidth(COLUMN_SEX, 0);
    ui->tableView_A->setColumnWidth(COLUMN_AGE, 0);
    ui->tableView_A->setColumnWidth(COLUMN_DEVICE,0);

}

void BattleGroup::initTableView_B()
{
    //自定义表头操作
    m_pTableHeader_Chlid = new TableViewHeader(Qt::Horizontal, this);//表头
    m_pTableHeader_Chlid->setStretchLastSection(true);//表头最后一列填充
    ui->tableView_B->setHorizontalHeader(m_pTableHeader_Chlid);//设置表头

    //创建model（目前使用的是标准数据模型，可以自定义数据模型），绑定model
    m_pTableModel_B = new QStandardItemModel(this);
    ui->tableView_B->setModel(m_pTableModel_B);//设置model
    ui->tableView_B->verticalHeader()->hide();


    //创建表头信息
    QStringList headerList;
    headerList << " " << tr("No.")   << tr("IP")<< tr("用户名") << tr("姓名") << tr("性别") << tr("年龄")<<tr("手环");
    m_pTableModel_B->setHorizontalHeaderLabels(headerList);

    //双击撤回
    connect(ui->tableView_B, &QTableView::doubleClicked,this, &BattleGroup::on_tableViewBDbClick);

    //设置列宽
    ui->tableView_B->setColumnWidth(COLUMN_CHECKBOX,0);
    ui->tableView_B->setColumnWidth(COLUMN_NO,50);
    ui->tableView_B->setColumnWidth(COLUMN_IP, 250);
    ui->tableView_B->setColumnWidth(COLUMN_ID, 250);

    ui->tableView_B->setColumnWidth(COLUMN_NAME, 250);
    ui->tableView_B->setColumnWidth(COLUMN_SEX, 0);
    ui->tableView_B->setColumnWidth(COLUMN_AGE, 0);
    ui->tableView_B->setColumnWidth(COLUMN_DEVICE,0);

}

void BattleGroup::initTableView_C()
{
    //自定义表头操作
    m_pTableHeader_Chlid = new TableViewHeader(Qt::Horizontal, this);//表头
    m_pTableHeader_Chlid->setStretchLastSection(true);//表头最后一列填充
    ui->tableView_C->setHorizontalHeader(m_pTableHeader_Chlid);//设置表头

    //创建model（目前使用的是标准数据模型，可以自定义数据模型），绑定model
    m_pTableModel_C = new QStandardItemModel(this);
    ui->tableView_C->setModel(m_pTableModel_C);//设置model
    ui->tableView_C->verticalHeader()->hide();


    //创建表头信息
    QStringList headerList;
    headerList << " " << tr("No.")   << tr("IP")<< tr("用户名") << tr("姓名") << tr("性别") << tr("年龄")<<tr("手环");
    m_pTableModel_C->setHorizontalHeaderLabels(headerList);

    //双击撤回
    connect(ui->tableView_C, &QTableView::doubleClicked,this, &BattleGroup::on_tableViewCDbClick);

    //设置列宽
    ui->tableView_C->setColumnWidth(COLUMN_CHECKBOX,0);
    ui->tableView_C->setColumnWidth(COLUMN_NO,50);
    ui->tableView_C->setColumnWidth(COLUMN_IP, 250);
    ui->tableView_C->setColumnWidth(COLUMN_ID, 250);

    ui->tableView_C->setColumnWidth(COLUMN_NAME, 250);
    ui->tableView_C->setColumnWidth(COLUMN_SEX, 0);
    ui->tableView_C->setColumnWidth(COLUMN_AGE, 0);
    ui->tableView_C->setColumnWidth(COLUMN_DEVICE,0);

}

void BattleGroup::initTableView_D()
{
    //自定义表头操作
    m_pTableHeader_Chlid = new TableViewHeader(Qt::Horizontal, this);//表头
    m_pTableHeader_Chlid->setStretchLastSection(true);//表头最后一列填充
    ui->tableView_D->setHorizontalHeader(m_pTableHeader_Chlid);//设置表头

    //创建model（目前使用的是标准数据模型，可以自定义数据模型），绑定model
    m_pTableModel_D = new QStandardItemModel(this);
    ui->tableView_D->setModel(m_pTableModel_D);//设置model
    ui->tableView_D->verticalHeader()->hide();


    //创建表头信息
    QStringList headerList;
    headerList << " " << tr("No.")   << tr("IP")<< tr("用户名") << tr("姓名") << tr("性别") << tr("年龄")<<tr("手环");
    m_pTableModel_D->setHorizontalHeaderLabels(headerList);

    //双击撤回
    connect(ui->tableView_D, &QTableView::doubleClicked,this, &BattleGroup::on_tableViewDDbClick);

    //设置列宽
    ui->tableView_D->setColumnWidth(COLUMN_CHECKBOX,0);
    ui->tableView_D->setColumnWidth(COLUMN_NO,50);
    ui->tableView_D->setColumnWidth(COLUMN_IP, 250);
    ui->tableView_D->setColumnWidth(COLUMN_ID, 250);

    ui->tableView_D->setColumnWidth(COLUMN_NAME, 250);
    ui->tableView_D->setColumnWidth(COLUMN_SEX, 0);
    ui->tableView_D->setColumnWidth(COLUMN_AGE, 0);
    ui->tableView_D->setColumnWidth(COLUMN_DEVICE,0);

}
/*************************
 * QTableView-单击事件
 * ***********************/
void BattleGroup::on_tableViewClick(QModelIndex nIndex)
{
    int nRow = nIndex.row();
    int nCol = nIndex.column();

    Qt::CheckState oldState = m_pTableModel_All->item(nRow, 0)->checkState();
    Qt::CheckState newState;

    if(oldState == Qt::Unchecked)
    {
        newState = Qt::Checked;
        m_nCheckedCount_All++;
    }
    else
    {
        newState = Qt::Unchecked;
        m_nCheckedCount_All--;
    }
    m_pTableModel_All->item(nRow, 0)->setCheckState(newState);

    if(newState == Qt::Unchecked || m_nCheckedCount_All == m_pTableModel_All->rowCount())
    {
        m_pTableHeader_All->onStateChanged(newState);//改变标题栏的全选状态
    }
}

/*************************
 * QTableView-双击事件
 * ***********************/
void BattleGroup::on_tableViewDbClick(QModelIndex nIndex)
{
    int row = nIndex.row();
}
/*************************
 * QTableView-双击事件
 * ***********************/
void BattleGroup::on_tableViewADbClick(QModelIndex nIndex)
{
    int row = nIndex.row();
    qDebug()<<row;

    m_userTableHash.insert(m_pTableModel_A->item(row,COLUMN_IP)->text(),m_userTableHash_A.find(m_pTableModel_A->item(row,COLUMN_IP)->text()).value());
    m_userTableHash_A.remove(m_pTableModel_A->item(row,COLUMN_IP)->text());
    RefreshTableA();
    RefreshTable();

}
/*************************
 * QTableView-双击事件
 * ***********************/
void BattleGroup::on_tableViewDDbClick(QModelIndex nIndex)
{
    int row = nIndex.row();
    qDebug()<<row;

    m_userTableHash.insert(m_pTableModel_D->item(row,COLUMN_IP)->text(),m_userTableHash_D.find(m_pTableModel_D->item(row,COLUMN_IP)->text()).value());
    m_userTableHash_D.remove(m_pTableModel_D->item(row,COLUMN_IP)->text());
    RefreshTableD();
    RefreshTable();
}
/*************************
 * QTableView-双击事件
 * ***********************/
void BattleGroup::on_tableViewBDbClick(QModelIndex nIndex)
{
    int row = nIndex.row();
    qDebug()<<row;

    m_userTableHash.insert(m_pTableModel_B->item(row,COLUMN_IP)->text(),m_userTableHash_B.find(m_pTableModel_B->item(row,COLUMN_IP)->text()).value());
    m_userTableHash_B.remove(m_pTableModel_B->item(row,COLUMN_IP)->text());
    RefreshTableB();
    RefreshTable();
}
/*************************
 * QTableView-双击事件
 * ***********************/
void BattleGroup::on_tableViewCDbClick(QModelIndex nIndex)
{
    int row = nIndex.row();
    qDebug()<<row;

    m_userTableHash.insert(m_pTableModel_C->item(row,COLUMN_IP)->text(),m_userTableHash_C.find(m_pTableModel_C->item(row,COLUMN_IP)->text()).value());
    m_userTableHash_C.remove(m_pTableModel_C->item(row,COLUMN_IP)->text());
    RefreshTableC();
    RefreshTable();
}
/*************************
 * QTableView-排序
 * ***********************/
void BattleGroup::on_tableViewSort(int nColumn)
{

    //点击表头实现排序
    ui->tableView->horizontalHeader()->setSortIndicatorShown(true);
    ui->tableView->sortByColumn(nColumn,Qt::SortOrder::AscendingOrder);
}
/*************************
 * QTableView-响应表头全选操作
 * ***********************/
void BattleGroup::on_tableViewSelectAllCheckBoxClick(Qt::CheckState state)
{
    int nRowCount = m_pTableModel_All->rowCount();//当前行数

    for(int i = 0; i < nRowCount; i++)
    {
        m_pTableModel_All->item(i, 0)->setCheckState(state);
    }
}

/*************************
 * QTableView-设置列宽
 * ***********************/
void BattleGroup::setColumnWidth()
{
    ui->tableView->setColumnWidth(COLUMN_CHECKBOX,30);
    ui->tableView->setColumnWidth(COLUMN_NO,20);
    ui->tableView->setColumnWidth(COLUMN_IP, 250);
    ui->tableView->setColumnWidth(COLUMN_ID, 150);

    ui->tableView->setColumnWidth(COLUMN_NAME, 150);
    ui->tableView->setColumnWidth(COLUMN_SEX, 80);
    ui->tableView->setColumnWidth(COLUMN_AGE, 80);
    ui->tableView->setColumnWidth(COLUMN_DEVICE,100);
}

void BattleGroup::on_pb_A_clicked()
{
    QHashUserTableIterator i = m_userTableHash.begin();
    int row = 0;
    QList<int>removeRows;
    for(; i != m_userTableHash.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();
        if(m_pTableModel_All->item(row,0)->checkState()==Qt::Checked)
        {
            removeRows.append(row);
            m_userTableHash_A.insert(data.IP,data);
        }
        row++;
    }

    for(int i=0;i<removeRows.size();i++)
    {
        m_userTableHash.remove(m_pTableModel_All->item(removeRows.at(i),COLUMN_IP)->text());
    }
    RefreshTable();
    RefreshTableA();
}

void BattleGroup::on_pb_B_clicked()
{
    QHashUserTableIterator i = m_userTableHash.begin();
    int row = 0;
    QList<int>removeRows;
    for(; i != m_userTableHash.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();
        if(m_pTableModel_All->item(row,0)->checkState()==Qt::Checked)
        {
            removeRows.append(row);
            m_userTableHash_B.insert(data.IP,data);
        }
        row++;
    }

    for(int i=0;i<removeRows.size();i++)
    {
        m_userTableHash.remove(m_pTableModel_All->item(removeRows.at(i),COLUMN_IP)->text());
    }
    RefreshTable();
    RefreshTableB();
}

void BattleGroup::on_pb_C_clicked()
{
    QHashUserTableIterator i = m_userTableHash.begin();
    int row = 0;
    QList<int>removeRows;
    for(; i != m_userTableHash.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();
        if(m_pTableModel_All->item(row,0)->checkState()==Qt::Checked)
        {
            removeRows.append(row);
            m_userTableHash_C.insert(data.IP,data);
        }
        row++;
    }

    for(int i=0;i<removeRows.size();i++)
    {
        m_userTableHash.remove(m_pTableModel_All->item(removeRows.at(i),COLUMN_IP)->text());
    }
    RefreshTable();
    RefreshTableC();
}

void BattleGroup::on_pb_D_clicked()
{
    QHashUserTableIterator i = m_userTableHash.begin();
    int row = 0;
    QList<int>removeRows;
    for(; i != m_userTableHash.end(); ++i)
    {
        USER_TABLE_DATA data = i.value();
        if(m_pTableModel_All->item(row,0)->checkState()==Qt::Checked)
        {
            removeRows.append(row);
            m_userTableHash_D.insert(data.IP,data);
        }
        row++;
    }

    for(int i=0;i<removeRows.size();i++)
    {
        m_userTableHash.remove(m_pTableModel_All->item(removeRows.at(i),COLUMN_IP)->text());
    }
    RefreshTable();
    RefreshTableD();
}

void BattleGroup::on_pushButton_exit_clicked()
{
    this->close();
}

void BattleGroup::on_pb_random_clicked()
{
    int userNums=m_userTableHash.size();
    QList<int> randomNumsIndex=generateUniqueRandomNumber(userNums);//随机索引
    int groupNum=userNums/4;
    for(int i=0;i<groupNum;i++)
    {
        m_userTableHash_A.insert(m_pTableModel_All->item(randomNumsIndex.at(i*4),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(i*4),COLUMN_IP)->text()).value());
        m_userTableHash_B.insert(m_pTableModel_All->item(randomNumsIndex.at(i*4+1),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(i*4+1),COLUMN_IP)->text()).value());
        m_userTableHash_C.insert(m_pTableModel_All->item(randomNumsIndex.at(i*4+2),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(i*4+2),COLUMN_IP)->text()).value());
        m_userTableHash_D.insert(m_pTableModel_All->item(randomNumsIndex.at(i*4+3),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(i*4+3),COLUMN_IP)->text()).value());
    }
    int remNum=userNums%4;
    if(remNum==1)
    {
        m_userTableHash_A.insert(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4),COLUMN_IP)->text()).value());
    }else if(remNum==2)
    {
        m_userTableHash_A.insert(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4),COLUMN_IP)->text()).value());
        m_userTableHash_B.insert(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4+1),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4+1),COLUMN_IP)->text()).value());
    }else if(remNum==3)
    {
        m_userTableHash_A.insert(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4),COLUMN_IP)->text()).value());
        m_userTableHash_B.insert(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4+1),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4+1),COLUMN_IP)->text()).value());
        m_userTableHash_C.insert(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4+2),COLUMN_IP)->text(),m_userTableHash.find(m_pTableModel_All->item(randomNumsIndex.at(groupNum*4+2),COLUMN_IP)->text()).value());
    }
    std::sort(randomNumsIndex.begin(),randomNumsIndex.end());//需要排序remove数据，否则索引发生错乱
    for(int i=userNums-1;i>-1;i--)
        m_userTableHash.remove(m_pTableModel_All->item(randomNumsIndex.at(i),COLUMN_IP)->text());
    RefreshTable();
    RefreshTableA();
    RefreshTableB();
    RefreshTableC();
    RefreshTableD();

}
QList<int> BattleGroup::generateUniqueRandomNumber(int userNums)
{
    int i,j;
    QList<int> listNums;
    srand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(i = 0; i < userNums; i++)
    {
        listNums.append(rand()%userNums);
        bool bFlag = true;
        while(bFlag)
        {
            for(j = 0; j < i; j++)
            {
                if(listNums[i] == listNums[j])
                {
                    break;
                }
            }
            if(j < i)
            {
                listNums[i]=  rand()%userNums;
            }
            if(j == i)
            {
                bFlag = !bFlag;
            }
        }
    }
    return listNums;
}

void BattleGroup::on_pb_enter_clicked()
{
    ItemSelectPage *itemSelectPage=new ItemSelectPage;
    connect(this,SIGNAL(SendUserTableInfoABCD(QHashUserTable,QHashUserTable,QHashUserTable,QHashUserTable)),
            itemSelectPage,SLOT(getUserTableInfoABCD(QHashUserTable,QHashUserTable,QHashUserTable,QHashUserTable)));
    itemSelectPage->show();
    emit SendUserTableInfoABCD(m_userTableHash_A,m_userTableHash_B,m_userTableHash_C,m_userTableHash_D);

    this->close();
}

